package com.ccjq.shosestore.order.service.impl;


import com.ccjq.shosestore.core.utils.PojoUtils;
import com.ccjq.shosestore.core.utils.UuidUtils;
import com.ccjq.shosestore.order.bo.OrderBo;
import com.ccjq.shosestore.order.bo.ShopInfoBo;
import com.ccjq.shosestore.order.dao.OrdersDao;
import com.ccjq.shosestore.order.dao.impl.OrdersDaoImpl;
import com.ccjq.shosestore.order.model.OrderModel;
import com.ccjq.shosestore.order.service.OrderService;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LCY
 * @create 2018-12-05 21:50
 */
public class OrderServiceImpl implements OrderService {

    /**
     * 根据用户id，查询订单概略信息
     * 仅展示OrdersModel模型中的orderId（订单编号）、createTime（生成时间）、amout（总金额）三条属性
     *
     * @param userId     用户唯一标识
     * @param currentRow 当前行
     * @return List<OrderBo> 订单模型的集合
     */
    public List<OrderBo> queryOrdersByUserId(String userId, int currentRow) {
        OrdersDao od = new OrdersDaoImpl();
        List<OrderModel> list = od.queryOrdersByUserId(userId, currentRow);
        List<OrderBo> result = new ArrayList<OrderBo>();
        for (OrderModel om : list) {
            result.add(PojoUtils.po2bo(om, OrderBo.class));
        }
        return result;
    }

    /**
     * 根据订单编号，查询指定订单详细信息
     * 商品详细信息使用List<String[]> shopInfo进行管理
     * 该数组存储信息结构：商品编号、商品描述、数量、价格、小计
     * 总金额为表的amout字段
     *
     * @param orderId 订单编号
     * @return OrderBo 包含订单详细信息的数据模型
     */
    public OrderBo queryOrdersByOrderId(String orderId) {
        OrdersDao od = new OrdersDaoImpl();
        OrderModel om = od.queryOrdersByOrderId(orderId);
        OrderBo ob = PojoUtils.po2bo(om, OrderBo.class);
        return ob;
    }


    public List<OrderBo> saveOrder(OrderBo om) throws SQLException {
        String createTime = om.getCreateTime();
        int payment = om.getPayment();
        double amount = om.getAmout();
        String receiver = om.getReceiver();
        String province = om.getProvince();
        String city = om.getCity();
        String county = om.getCounty();
        String address = om.getAddress();
        String zipcode = om.getZipcode();
        String telphone = om.getTelphone();
        String userId = om.getUserId();
        String uuid = UuidUtils.generateShortUUID("to", 9);

        String sql1 = "INSERT INTO T_ORDER VALUES(" +
                "'" + uuid + "'," +

                createTime + ","

                + payment + "," +
                +amount + "," +
                "'" + receiver + "'," +
                "'" + province + "'," +
                "'" + city + "'," +
                "'" + county + "'," +
                "'" + address + "'," +
                "'" + zipcode + "'," +
                "'" + telphone + "'," +
                "'" + userId + "'" +
                ")";

        //sql2准备拼接

        StringBuilder sb = new StringBuilder();
        List<ShopInfoBo> list = om.getShopInfo();
        for (ShopInfoBo si : list) {
            sb.append("INSERT INTO T_ORDER_PRODUCT VALUES (");
            sb.append("'" + uuid + "',");
            sb.append("'" + si.getProduceId() + "',");
            sb.append("" + si.getQuantity() + ",");
            sb.append("" + si.getPrice() + "");
            sb.append(");");
        }
        String sql2 = sb.toString();

        OrdersDao od = new OrdersDaoImpl();
        List<OrderModel> tempResult = od.saveOrder(sql1, sql2, userId);
        List<OrderBo> result = new ArrayList<OrderBo>();
        for (OrderModel orderModel : tempResult) {
            result.add(PojoUtils.po2bo(orderModel, OrderBo.class));
        }
        return result;
    }

    public int getTotalRows(String userId) {
        OrdersDao ordersDao = new OrdersDaoImpl();
        int totalRows = ((OrdersDaoImpl) ordersDao).getTotalRows(userId);
        return totalRows;
    }
}
